package com.ezops.dtcc.model.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.jdbc.core.JdbcTemplate;

import com.ezops.dtcc.model.DtccException;
import com.ezops.dtcc.model.DtccPosition;
import com.ezops.dtcc.rowmapper.DtccExceptionRowMapper;

public class DtccExceptionDAO implements ApplicationContextAware {
	
	@SuppressWarnings("unused")
	private ApplicationContext context;
	
	private final static Logger LOGGER = Logger.getLogger(DtccExceptionDAO.class);
	private JdbcTemplate jdbcTemplate;

	public DtccExceptionDAO(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	
	public void addPositionExceptions(int ruleId, List<DtccPosition> postionsList){
		
		for(DtccPosition dtccPosition: postionsList){
			DtccException dtccException = new DtccException();
			dtccException.setDtccRecId(dtccPosition.getDtccRecId());
			dtccException.setRuleId(ruleId);
			this.insert(dtccException);
		}
		
	}
	
	
	public void insert(DtccException dtccException) {
		
		String insertSql = "INSERT INTO DTCC_EXECEPTION ("
				+ "EXCP_ID, RULE_ID, DTCC_REC_ID, EXECUTION_DT)";
		insertSql += " VALUES(" 
				+ "DTCC_EXCEPTION_SEQ.NEXTVAL,?,?,SYSDATE)";
		
		LOGGER.info("Inserting Record Exception for DTCC_REC_ID ->" + dtccException.getDtccRecId());
		getJdbcTemplate().update(
				insertSql,
				new Object[] { dtccException.getRuleId(), dtccException.getDtccRecId() } );
	
	}
	
	public List<DtccException> findAll() {
		return null; //NO OP
	}
	
	
	public List<DtccException> findAllByRuleId(int ruleId) {
		
		String selectDttcExceptions = "SELECT * DTCC_EXCEPTION WHERE RULE_ID =" + ruleId;
		List<Object> resultList = this.jdbcTemplate.query(selectDttcExceptions,  new DtccExceptionRowMapper());
		List<DtccException> exceptionList = new ArrayList<DtccException>();
		if (resultList != null && !resultList.isEmpty()) {
		    for (int i = 0; i < resultList.size(); i++) {
		    	exceptionList.add((DtccException) resultList.get(i));		
		    }
		}	
		return exceptionList;
	}		
		
	
	
	public DtccException findByExceptionId(int exceptionId) {

		DtccException foundItem = null;
		String selectRule = "SELECT * DTCC_EXCEPTION WHERE EXCP_ID " + exceptionId;
		List<Object> resultList = this.jdbcTemplate.query(selectRule,  new DtccExceptionRowMapper());
		if(!resultList.isEmpty() ) 
			foundItem = (DtccException)resultList.get(0);
		
		return foundItem;
		
	}
	
	
	
	
	@Override
	public void setApplicationContext(ApplicationContext beanCtx) 	throws BeansException {
		this.context = beanCtx;
		
	}
	
	
	

}
